package com.dataart.productshop.file.utils;

import java.io.File;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {
	public static String getString(){
		return new BigInteger(new SecureRandom().nextInt(150) + 150, new SecureRandom()).toString(32);
	}
	
	public static String nextNextFolderPath(String initPath){
		String newFoldername = "";
		File file = null;
		while (true){
			newFoldername = StringUtils.getString();
			file = new File(initPath + "/" + newFoldername);
			if(!file.exists()){
				file.mkdir();
				break;
			} else {
				
			}
		}
		return newFoldername;
	}

    public static void rollbackFolderCreation(String folderpath){
        File fileToDel = new File(folderpath);
        fileToDel.delete();
    }
	
	public static List<String> getRegesps(String container, String regexp){
		Pattern pattern = Pattern.compile(regexp);
		Matcher matcher = pattern.matcher(container);
		List<String> matchesParts = new ArrayList<String>();
		
		while(matcher.find()){
			matchesParts.add(matcher.group());
		}
		
		return matchesParts;
	}
	
	public static String getFirstExpr(String container, String regexp){
		List<String> exps = getRegesps(container, regexp);
		return exps.isEmpty() ? "" : exps.get(0);
	}

    public static boolean isEmpty(String s){
        return s == null || s.isEmpty();
    }
	
	
}
